package com.xhs.manager.impl;


import com.xhs.bo.SmsBo;
import com.xhs.config.RedisKeyConfig;
import com.xhs.config.SystemConfig;
import com.xhs.entity.SmsLog;
import com.xhs.manager.intf.AliSmsService;
import com.xhs.service.intf.SmsLogService;
import com.xhs.utils.*;
import com.xhs.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AliSmsServiceImpl implements AliSmsService {

    @Autowired
    private SmsLogService smsLogService;

    @Override
    public R sendRCode(String phone) {
        //检验是否为空
        if (StringUtil.isNotEmpty(phone)){
            //检验手机号是否正确
            if (phone.matches("^1{1}[3-9]{1}[0-9]{9}$")){
                //生成验证码
                int code = NumUtil.createNum(4);
                System.out.println(code);
                //发送验证码
//                if (AliSmsUntil.sendRegisterCode(phone,code)){
                    if (true){
                    //成功就保存到Redis
                    JedisUtil.addStr(RedisKeyConfig.SMS_RCODE+phone,code+"",RedisKeyConfig.SMS_RCODE_TIME);
                    //记录数据到数据库
                    smsLogService.save(new SmsLog(phone, SystemConfig.SMS_TYPE_RCODE,"新注册",new Date()));
                    //返回
                    return RUtil.ok("发送成功",null);
                }
                return RUtil.fail("验证码发送失败");
            }
            return RUtil.fail("手机号错误");
        }
        return RUtil.fail("手机号未填写");
    }

    @Override
    public R checkRCode(SmsBo bo) {
        //入参校验
        if (bo!=null){
            if (StringUtil.isNotEmpty(bo.getPhone())&&bo.getPhone().matches("^1{1}[3-9]{1}[0-9]{9}$")){
                String key = RedisKeyConfig.SMS_RCODE+ bo.getPhone();
                if (JedisUtil.checkKey(key)){
                    if (bo.getCode()==Integer.parseInt(JedisUtil.getStr(key))){
                        JedisUtil.delKey(key);
                        JedisUtil.addSet(RedisKeyConfig.REGISTER_PHONES,bo.getPhone());
                        return RUtil.ok(null);
                    }
                }
            }
        }
        return RUtil.fail("验证码错误");
    }

    @Override
    public R sendLCode(String phone) {
        //检验是否为空
        if (StringUtil.isNotEmpty(phone)){
            //检验手机号是否正确
            if (phone.matches("^1{1}[3-9]{1}[0-9]{9}$")){
                //生成验证码
                int code = NumUtil.createNum(4);
                //发送验证码
                if (AliSmsUntil.sendRegisterCode(phone,code)){
                    //成功就保存到Redis
                    JedisUtil.addStr(RedisKeyConfig.SMS_LCODE+phone,code+"",RedisKeyConfig.SMS_LCODE_TIME);
                    //记录数据到数据库
                    smsLogService.save(new SmsLog(phone, SystemConfig.SMS_TYPE_LCODE,"登录",new Date()));
                    //返回
                    return RUtil.ok("发送成功",null);
                }
                return RUtil.fail("验证码发送失败");
            }
            return RUtil.fail("手机号错误");
        }
        return RUtil.fail("手机号未填写");
    }

    @Override
    public R sendFCode(String phone) {
        //检验是否为空
        if (StringUtil.isNotEmpty(phone)){
            //检验手机号是否正确
            if (phone.matches("^1{1}[3-9]{1}[0-9]{9}$")){
                //生成验证码
                int code = NumUtil.createNum(4);
                System.out.println(code);
                //发送验证码
//                if (AliSmsUntil.sendRegisterCode(phone,code)){
                    if (true){
                    //成功就保存到Redis
                    JedisUtil.addStr(RedisKeyConfig.SMS_FCODE+phone,code+"",RedisKeyConfig.SMS_LCODE_TIME);
                    //记录数据到数据库
                    smsLogService.save(new SmsLog(phone, SystemConfig.SMS_TYPE_FCODE,"找回密码",new Date()));
                    //返回
                    return RUtil.ok("发送成功",null);
                }
                return RUtil.fail("验证码发送失败");
            }
            return RUtil.fail("手机号错误");
        }
        return RUtil.fail("手机号未填写");
    }
}
